package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 用户管理控制器
 */
@Slf4j
@RestController
@Tag(name = "用户管理模块")
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.注册功能
     */
    @PostMapping("reg")
    @Operation(summary = "注册功能")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("userRegParam = " + userRegParam);
        /*
            1.接收客户端传递的数据[已完成];
            2.数据合法性校验[此处暂时不做,后期使用验证框架完成];
            3.校验用户名是否被占用[SELECT接口];
              SELECT COUNT(*) FROM user WHERE username = ?
              3.1 被占用: return new JsonResult(StatusCode.USERNAME_EXISTS);
              3.2 未被占用: 插入1条数据[INSERT接口], return JsonResult.ok();
         */
        String username = userRegParam.getUsername();
        int i = userMapper.selectByUsername(username);
        if (i > 0){//用户名被占用
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        //未被占用
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }

    /**
     * 2.登录功能
     */
    @PostMapping("login")
    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("userLoginParam = " + userLoginParam);
        /*
            1.获取客户端传递的数据[已完成];
            2.查询用户名和密码是否正确[SELECT接口]
              SELECT id,username FROM user WHERE username = ? AND password = ?
            3.查询结果
              查询到了, userVO: {"id":101, "username":"赵丽颖"}
              未查询到, userVO: null
         */
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if (userVO != null){//登录成功
            //将用户信息保存到session中[保存到服务器端的内存中了]
            session.setAttribute("user", userVO);
            return JsonResult.ok(userVO);
        }
        //用户名或密码错误
        return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
    }

    /**
     * 3.获取当前用户功能
     */
    @GetMapping("currentUser")
    @Operation(summary = "获取当前用户功能")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        //JsonResult: {"state":2000, "message":"操作成功", "data": userVO}
        //前端工程师:利用JavaScript语言,判断用户登录状态
        //if(response.data.data!=null){以登录状态跳转到首页,显示欢迎你,xxx}
        //else{重定向到登录页面}
        return JsonResult.ok(userVO);
    }

    /**
     * 4.退出登录功能
     *   4.1 登录成功;
     *   4.2 获取当前用户[可以获取到数据];
     *   4.3 退出登录;
     *   4.4 获取当前用户[获取不到数据];
     */
    @GetMapping("logout")
    @Operation(summary = "退出登录功能")
    @ApiOperationSupport(order = 40)
    public void logout(HttpSession session){
        //在服务器内存中删除会话标识
        session.removeAttribute("user");
    }

    /**
     * 5.登录功能2:该方法仅仅是为了演示 HttpServletRequest 使用;
     */
    @GetMapping("login2")
    public JsonResult login2(HttpServletRequest request){
        //获取请求方法
        String method = request.getMethod();
        //获取请求路径
        String pathInfo = request.getRequestURI();
        //获取请求体数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //获取会话
        HttpSession session = request.getSession();
        //获取客户端IP地址
        String remoteAddr = request.getRemoteAddr();
        log.debug("method = " + method);
        log.debug("pathInfo = " + pathInfo);
        log.debug("username = " + username);
        log.debug("password = " + password);
        log.debug("remoteAddr = " + remoteAddr);
        log.debug("session = " + session);

        return JsonResult.ok();
    }
}














